#include <iostream>
#include <vector>
#include <queue>

using namespace std;
priority_queue<int, vector<int>, greater<int>> hp;
int n;
int ret;

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		int x; cin >> x;
		hp.push(x);
	}
	
	while(hp.size() > 1)
	{
		int x = hp.top(); hp.pop();
		int y = hp.top(); hp.pop();
		ret += x + y;
		hp.push(x + y);
	}
	
	cout << ret << endl;
	
	return 0;
}
